// pages/test/test.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ip: "IP地址", //IP地址
        port: null, //端口号
        message: "", //发送的数据
        strs: "", //接收到的数据
        msg: "发送数据成功",
        success: false,
        swish: false,
        switch1Checked: false,
        switch2Checked: false,
        switch3Checked: false,
        switch4Checked: false,
        switch5Checked: false,
        switch6Checked: false,
        switch7Checked: false,
        text: "",
        showOneButtonDialog: false,
        oneButton: [{
            text: '确定'
        }],
        dialogShow: false,
    },

        /* 跳转设置页面 */
        tocolor: function () {
            wx.navigateTo({
                url: "../color/color",
            });
        },
    
    /** 语音识别开始 */

    startRecord: function () {
        var plugin = requirePlugin("WechatSI")
        let manager = plugin.getRecordRecognitionManager()
        let that = this
        manager.onRecognize = function (res) {
            console.log("current result", res.result)
        }
        manager.onStop = function (res) {
            console.log("record file path", res.tempFilePath)
            // console.log("result", res.result)
            // that.data.Msgresult = res.result
            that.setData({
                text: res.result
            })
            that.setData({
                message: res.result
            })
            console.log("current result", that.data.message)
        }
        manager.onStart = function (res) {
            console.log("成功开始录音识别", res)
        }
        manager.onError = function (res) {
            console.error("error msg", res.msg)
        }
        manager.start({
            duration: 30000,
            lang: "zh_CN"
        })
    },

    /** 语音识别结束 */

    startRecordEnd: function () {
        var plugin = requirePlugin("WechatSI")
        let manager = plugin.getRecordRecognitionManager()
        manager.stop({})
    },


    /* 跳转设置页面 */
    tosettings: function () {
        wx.navigateTo({
            url: "../settings/settings",
        });
    },


    /**转换字符 */
    uintToString: function uintToString(uintArray) {
        var encodedString = String.fromCharCode.apply(null, uintArray),
            decodedString = decodeURIComponent(escape(encodedString));
        return decodedString;
    },


    //显示提示函数swish
    pushDataSuccess: function () {
        wx.showToast({
            title: this.data.strs,
            icon: 'sucess',
            image: '',
            duration: 800,
            mask: true,
        });
        setTimeout(function () {
            wx.hideToast();
        }, 500);
        // this.setData({
        //     swish: true
        // })
    },


    /*发送文本UDP字节函数*/

    pushTheData: function pushTheData(msg) {
        const udp = wx.createUDPSocket()
        // udp.close()
        udp.bind()
        udp.send({
            address: this.data.ip,
            port: this.data.port,
            length: 1024,
            message: msg,
        })
        console.log(msg);
        udp.onError()

        udp.offMessage()
        let that = this
        udp.onMessage(function (res) {
            // console.log(res)
            // console.log(res.message)
            var buf = res.message
            var decodedString = String.fromCharCode.apply(null, new Uint8Array(buf));
            let str = decodedString
            console.log('str == ' + str)
            // that.setData({
            //     strs: str,
            // })
            that.data.strs = str
            console.log("that.data.strs is " + that.data.strs)
            that.pushDataSuccess()
        })
        // udp.offMessage()
    },

    /* LED 1 */
    switch1Change: function () {
        this.data.switch1Checked = !this.data.switch1Checked;
        if (this.data.switch1Checked) {
            this.pushTheData(getApp().globalData.LED1_on)
        } else {
            this.pushTheData(getApp().globalData.LED1_off)
        }
    },

    /* LED 2 */
    switch2Change: function () {
        this.data.switch2Checked = !this.data.switch2Checked;
        if (this.data.switch2Checked) {
            this.pushTheData(getApp().globalData.LED2_on)
        } else {
            this.pushTheData(getApp().globalData.LED2_off)
        }
    },


    /* LED 3 */
    switch3Change: function () {
        this.data.switch3Checked = !this.data.switch3Checked;
        if (this.data.switch3Checked) {
            this.pushTheData(getApp().globalData.LED3_on)
        } else {
            this.pushTheData(getApp().globalData.LED3_off)
        }
    },


    /* LED 4 */
    switch4Change: function () {
        this.data.switch4Checked = !this.data.switch4Checked;
        if (this.data.switch4Checked) {
            this.pushTheData(getApp().globalData.LED4_on)
        } else {
            this.pushTheData(getApp().globalData.LED4_off)
        }
    },



    /* LED 5 */
    switch5Change: function () {
        this.data.switch5Checked = !this.data.switch5Checked;
        if (this.data.switch5Checked) {
            this.pushTheData(getApp().globalData.LED5_on)
        } else {
            this.pushTheData(getApp().globalData.LED5_off)
        }
    },


    /* LED 6 */
    switch6Change: function () {
        this.data.switch6Checked = !this.data.switch6Checked;
        if (this.data.switch6Checked) {
            this.pushTheData(getApp().globalData.LED6_on)
        } else {
            this.pushTheData(getApp().globalData.LED6_off)
        }
    },


    /* LED 7 */
    switch7Change: function () {
        this.data.switch7Checked = !this.data.switch7Checked;
        if (this.data.switch7Checked) {
            this.pushTheData(getApp().globalData.LED7_on)
        } else {
            this.pushTheData(getApp().globalData.LED7_off)
        }
    },

    /* 替换输入栏内数据*/
    MsgInput: function (e) {
        this.setData({
            message: e.detail.value
        })
    },

    //显示提示函数
    DataSuccess: function () {
        this.setData({
            success: true
        })
    },

    /* 点击确定后做的事 */
    tapDialogButton(e) {
        this.setData({
            dialogShow: false,
            showOneButtonDialog: false
        })
    },


    //按钮发送数据
    MsgOutput: function () {
        if (this.data.message == "") {
            this.setData({
                showOneButtonDialog: true
            })
        } else {
            this.setData({
                dialogShow: false,
                showOneButtonDialog: false
            })
            this.pushTheData(this.data.message)
            this.setData({
                'text': '',
            })
            this.setData({
                'message': ''
            })
            this.DataSuccess()
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //初始化数据
        this.data.ip = getApp().globalData.ip
        this.data.port = getApp().globalData.port
        console.log(this.data.ip);
        console.log(this.data.port);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        wx.showLoading({
            title: "加载中",
        });

        // 发送UDP字节函数
        const udp = wx.createUDPSocket();
        udp.bind(10000);
        udp.send({
            address: this.data.ip,
            port: this.data.port,
            length: 1024,
            message: 'I am Ready',
        });
        console.log("I am Ready");

        let that = this
        udp.onMessage(function (res) {
            console.log(res)
            console.log(res.message)
            var buf = res.message
            var decodedString = String.fromCharCode.apply(null, new Uint8Array(buf));
            let str = decodedString
            console.log('str===' + str)
            that.data.strs = str
            console.log(that.data.strs)
        })

        setTimeout(function () {
            wx.hideLoading(); //必须调用hideLoading才能关闭loading
        }, 2000);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})